﻿@{
    ViewBag.Title = "活动室列表";
}
<div class="table-wraper">
    <div class="table-header">
        <h3 class="table-title">
            活动室列表<small>&nbsp;&nbsp;<button type="button" id="btnAdd" class="btn btn-primary btn-sm">添加活动室</button></small>
        </h3>
        <div class="form-inline" style="margin: 0.6em 0">
            <div class="form-group">
                <input type="search" value="" id="placeName" placeholder="活动室名称" class="form-control" />
            </div>
            <button id="btnSearch" type="button" class="btn btn-success">搜索</button>
        </div>
    </div>
    <div class="table-body" id="div_main"></div>
</div>
<div id="editPlace" style="padding: 1em 2.2em; display: none">
    <form>
        <div class="input-group">
            <label for="inputPlaceName">请输入要活动室名称</label> <br />
            <input type="text" required="required" class="form-control" value="" id="inputPlaceName" placeholder="活动室名称" />
        </div>
    </form>
</div>
@section scripts{
    <script>
        var pageSize = 10, pageIndex = 1;

        //加载数据
        function loadData(pIndex) {
            if (!pIndex) {
                pageIndex = 1;
            } else {
                pageIndex = pIndex;
            }
            $("#div_main").html('<br /><img src="/images/loading.gif" /> 正在玩命加载中，请稍后...');
            $.ajax({
                url: "/Admin/ReservationPlace/List",
                data: {
                    "pageIndex": pageIndex,
                    "pageSize": pageSize,
                    "placeName": $("#placeName").val()
                },
                success: function (data, status) {
                    if (status == "success") {
                        $("#div_main").html(data);
                    } else {
                        $("#div_main").html('');
                        layer.msg('数据加载异常，请稍后重试！');
                    }
                }
            });
        }

        //add place
        $("#btnAdd").on("click",
            function () {
                $("#inputPlaceName").val('');
                $("#btnAdd").attr("disabled", "disabled");
                layer.open({
                    type: 1,
                    title: "添加活动室",
                    btn: ["添加", "取消"],
                    content: $("#editPlace"),
                    yes: function (index) {
                        var placeName = $("#inputPlaceName").val();
                        if (!placeName) {
                            layer.msg("活动室名称不能为空");
                            return;
                        }
                        $.post("/Admin/ReservationPlace/AddPlace",
                            { "placeName": placeName },
                            function (data) {
                                if (!data) {
                                    loadData();
                                    $("#btnAdd").removeAttr("disabled");
                                    layer.close(index);
                                } else {
                                    layer.msg(data);
                                }
                            });
                    },
                    btn2: function (index) {
                        $("#btnAdd").removeAttr("disabled");
                        layer.close(index);
                    },
                    cancel: function () {
                        $("#btnAdd").removeAttr("disabled");
                    }
                });
            });

        //edit placeName
        function editPlace(placeId, placeName, obj) {
            $("#inputPlaceName").val(placeName);
            //disabled
            $(obj).attr("disabled", "disabled");
            //layer
            layer.open({
                type: 1,
                title: "编辑活动室名称",
                btn: ["更新", "取消"],
                content: $("#editPlace"),
                yes: function (index) {
                    var newPlaceName = $("#inputPlaceName").val();
                    if (!newPlaceName) {
                        layer.msg('活动室名称不能为空');
                        return;
                    }
                    $.post("/Admin/ReservationPlace/UpdatePlaceName",
                        { "placeId": placeId, "beforeName": placeName, "newName": newPlaceName },
                        function (data) {
                            if (!data) {
                                loadData(pageIndex);
                                $(obj).removeAttr("disabled");
                                layer.close(index);
                            } else {
                                layer.msg(data);
                            }
                        });
                },
                btn2: function (index) {
                    $(obj).removeAttr("disabled");
                    layer.close(index);
                },
                cancel: function (index) {
                    $(obj).removeAttr("disabled");
                    layer.close(index);
                }
            });
        }

        //delete
        function deletePlace(placeId, placeName, obj) {
            //disable
            $(obj).attr("disabled", "disabled");
            layer.confirm("确定要删除活动室【" + placeName + "】吗？",
                {
                    title: "警告"
                },
                function (index) {
                    $.post("/Admin/ReservationPlace/DeletePlace",
                        { "placeId": placeId, "placeName": placeName },
                        function (data) {
                            if (!data) {
                                loadData(pageIndex);
                                $(obj).removeAttr("disabled");
                                layer.close(index);
                            } else {
                                layer.msg(data);
                            }
                        });
                },
                function (index) {
                    $(obj).removeAttr("disabled");
                    layer.close(index);
                });
        }

        //update status
        function updatePlaceStatus(placeId, placeName, status, obj) {
            $(obj).attr("disabled", "disabled");
            var oper = (parseInt(status) > 0 ? "启用" : "禁用");
            layer.confirm("将要 " + oper + " 活动室【" + placeName + "】，是否继续？",
                { title: "警告" },
                function (index) {
                    $.post("/Admin/ReservationPlace/UpdatePlaceStatus",
                        { "placeId": placeId, "placeName": placeName, "status": status },
                        function (data) {
                            if (!data) {
                                loadData(pageIndex);
                                $(obj).removeAttr("disabled");
                                layer.close(index);
                            } else {
                                layer.msg(data);
                            }
                        });
                },
                function () {
                    $(obj).removeAttr("disabled");
                });
        }

        //搜索
        $("#btnSearch").on("click",
            function () {
                loadData();
            });
        $(function () {
            loadData();
        });
    </script>
}